home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / libs / winlib-0.0 / winlib-0 / win / sound / main.c next >
Encoding:
C/C++ Source or Header  |  1995-12-25  |  4.6 KB  |  211 lines

  1. /*****************************************************************************
  2.  *                                                                           *
  3.  * Sound Server version 0.0.1 by Ken Hollis <khollis@bitgate.com>            *
  4.  * Copyright (C) 1995-1996, Bitgate Software                                 *
  5.  *                                                                           *
  6.  * This sound server is released under the Linux Gnu Public License.  This   *
  7.  * program is not for resale, and may not be modified without releasing      *
  8.  * patches.                                                                  *
  9.  *                                                                           *
  10.  *****************************************************************************/
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <netdb.h>
  16. #include <unistd.h>
  17. #include <syslog.h>
  18. #include <fcntl.h>
  19. #include <pwd.h>
  20. #include <ctype.h>
  21. #include <sys/signal.h>
  22. #include <sys/socket.h>
  23. #include <sys/time.h>
  24. #include <sys/wait.h>
  25. #include <sys/errno.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/types.h>
  28. #include <arpa/inet.h>
  29. #include <netinet/in.h>
  30.  
  31. #include "config.h"
  32.  
  33. #define    SERVER_PORT    8000
  34.  
  35. int server_socket, sockets[16];
  36. fd_set readset, activeset;
  37.  
  38. void Server_Initialize(void)
  39. {
  40.     struct protoent *p;
  41.     struct sockaddr_in sin;
  42.     int i;
  43.  
  44.     for(i = 0; i < 16; i++)
  45.     sockets[i] = -1;
  46.  
  47.     bzero((char *) &sin, sizeof(sin));
  48.     sin.sin_family = AF_INET;
  49.     sin.sin_addr.s_addr = INADDR_ANY;
  50.  
  51.     if ((sin.sin_port = htons(SERVER_PORT)) == 0) {
  52.     printf("Sound server cannot initialize - port 8000 cannot be found!\n");
  53.     exit(0);
  54.     }
  55.  
  56.     if ((p = getprotobyname("tcp")) == 0) {
  57.     printf("Sound server cannot initialize - Protocol \"tcp\" unknown.\n");
  58.     exit(0);
  59.     }
  60.  
  61.     if ((server_socket = socket(AF_INET, SOCK_STREAM, p->p_proto)) < 0) {
  62.     printf("Sound server cannot initialize - socket creation impossible\n");
  63.     exit(0);
  64.     }
  65.  
  66.     if (bind(server_socket, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
  67.     printf("Sound server cannot initialize - could not bind!\n");
  68.     exit(0);
  69.     }
  70.  
  71.     if (listen(server_socket, 5) < 0) {
  72.     printf("Sound server cannot initialize - could not listen!\n");
  73.     exit(0);
  74.     }
  75.  
  76.     printf("Sound server 0.0.1 started.\n");
  77. }
  78.  
  79. int add_connection(int sock)
  80. {
  81.     int i;
  82.  
  83.     for(i = 0; i < 16; i++)
  84.     if (sockets[i] == -1) {
  85.         sockets[i] = sock;
  86.         return(1);
  87.     }
  88.  
  89.     return(0);
  90. }
  91.  
  92. void remove_connection(int sock)
  93. {
  94.     int i;
  95.  
  96.     for(i = 0; i < 16; i++)
  97.     if (sockets[i] == sock) {
  98.         sockets[i] = -1;
  99.         return;
  100.     }
  101. }
  102.  
  103. void handle_data(char *cmd)
  104. {
  105.     char command, buffer[8000];
  106.  
  107.     bzero(buffer, 8000);
  108.     sscanf(cmd, "%c%s\r\n", &command, buffer);
  109.  
  110.     if (command == 'P') {
  111.     /* Play a Sun Audio sound file, if this is one. */
  112.     if (strstr(buffer, ".au")) {
  113.         int f1, f2;
  114.         char bleah[512];
  115.  
  116.         f1 = open("/dev/audio", O_WRONLY);
  117.         f2 = open(buffer, O_RDONLY);
  118.  
  119.         while((read(f2, &bleah, 512)) > 0)
  120.         write(f1, &bleah, 512);
  121.  
  122.         close(f1);
  123.         close(f2);
  124.     }
  125.  
  126.     /* Play Microsoft WAV file, if this is one. */
  127.     if (strstr(buffer, ".wav")) {
  128.         char fn[160];
  129.  
  130.         bzero(fn, 160);
  131.         sprintf(fn, "%s %s", WAVFILE, buffer);
  132.         system(fn);
  133.     }
  134.     }
  135. }
  136.  
  137. void handle_client(int i)
  138. {
  139.     int size;
  140.     char buffer[8192];
  141.  
  142.     bzero(buffer, 8192);
  143.     size = read(sockets[i], buffer, 8192);
  144.  
  145.     if (size <= 0) {
  146.     (void) shutdown(sockets[i], 2);
  147.     close(sockets[i]);
  148.  
  149.     FD_CLR(sockets[i], &readset);
  150.     FD_CLR(sockets[i], &activeset);
  151.  
  152.     remove_connection(i);
  153.     return;
  154.     } else {
  155.     handle_data(buffer);
  156.     }
  157. }
  158.  
  159. void Server_Loop(void)
  160. {
  161.     int fds = getdtablesize(), i;
  162.     struct sockaddr_in sin;
  163.  
  164.     FD_ZERO(&activeset);
  165.     FD_SET(server_socket, &activeset);
  166.  
  167.     while(1) {
  168.     struct timeval t;
  169.  
  170.     t.tv_sec = 0;
  171.     t.tv_usec = 100;
  172.  
  173.     bcopy(&activeset, &readset, sizeof(readset));
  174.  
  175.     if (select(fds, &readset, NULL, NULL, &t) < 0) {
  176.         fprintf(stderr, "(Sound server reports a select error)\n");
  177.     }
  178.  
  179.     if (FD_ISSET(server_socket, &readset)) {
  180.         int addrlen, newsock;
  181.  
  182.         addrlen = sizeof(struct sockaddr_in);
  183.         newsock = accept(server_socket, (struct sockaddr *) &sin, &addrlen);
  184.  
  185.         if (newsock < 0) {
  186.         fprintf(stderr, "(Sound server reports a socket accept error)\n");
  187.         } else {
  188.         FD_SET(newsock, &activeset);
  189.  
  190.         if (!add_connection(newsock)) {
  191.             (void) shutdown(newsock, 2);
  192.             close(newsock);
  193.             FD_CLR(newsock, &activeset);
  194.             FD_CLR(newsock, &readset);
  195.         }
  196.         }
  197.     }
  198.  
  199.     for(i = 0; i < 16; i++)
  200.         if (sockets[i] != -1)
  201.         if (FD_ISSET(sockets[i], &readset))
  202.             handle_client(i);
  203.     }
  204. }
  205.  
  206. void main(int argc, char *argv[])
  207. {
  208.     Server_Initialize();
  209.     Server_Loop();
  210. }
  211.